package com.yimu.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yimu.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.yimu.system.domain.Devices;
import com.yimu.system.mapper.OrdersMapper;
import com.yimu.system.domain.Orders;
import com.yimu.system.service.IOrdersService;
import com.yimu.common.core.text.Convert;

/**
 * 订单测试Service业务层处理
 * 
 * @author Christine
 * @date 2020-08-26
 */
@Service
public class OrdersServiceImpl implements IOrdersService 
{
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 查询订单测试
     * 
     * @param Id 订单测试ID
     * @return 订单测试
     */
    @Override
    public Orders selectOrdersById(Long Id)
    {
        return ordersMapper.selectOrdersById(Id);
    }

    /**
     * 查询订单测试列表
     * 
     * @param orders 订单测试
     * @return 订单测试
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders)
    {
        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 新增订单测试
     * 
     * @param orders 订单测试
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOrders(Orders orders)
    {
        int rows = ordersMapper.insertOrders(orders);
        insertDevices(orders);
        return rows;
    }

    /**
     * 修改订单测试
     * 
     * @param orders 订单测试
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOrders(Orders orders)
    {
        ordersMapper.deleteDevicesByDeviceNum(orders.getId());
        insertDevices(orders);
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 删除订单测试对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrdersByIds(String ids)
    {
        ordersMapper.deleteDevicesByDeviceNums(Convert.toStrArray(ids));
        return ordersMapper.deleteOrdersByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除订单测试信息
     * 
     * @param Id 订单测试ID
     * @return 结果
     */
    @Override
    public int deleteOrdersById(Long Id)
    {
        ordersMapper.deleteDevicesByDeviceNum(Id);
        return ordersMapper.deleteOrdersById(Id);
    }

    /**
     * 新增终端列表信息
     * 
     * @param orders 订单测试对象
     */
    public void insertDevices(Orders orders)
    {
        List<Devices> devicesList = orders.getDevicesList();
        Long Id = orders.getId();
        if (StringUtils.isNotNull(devicesList))
        {
            List<Devices> list = new ArrayList<Devices>();
            for (Devices devices : devicesList)
            {
                devices.setId(Id);
                list.add(devices);
            }
            if (list.size() > 0)
            {
                ordersMapper.batchDevices(list);
            }
        }
    }
}
